package com.example.administrator.caigou51.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.util.AbImageUtil;
import com.ab.util.AbLogUtil;
import com.ab.util.AbToastUtil;
import com.ab.util.AbViewUtil;

import com.example.administrator.caigou51.EventBusClass.GotoFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateBlurImage;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.control.ChangeTextIcon;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.AppManager;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListAdapter;
import com.example.administrator.caigou51.util.AnimUtils;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.Blur;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.umeng.analytics.MobclickAgent;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

;import cn.jpush.android.api.JPushInterface;
import cn.xiaoneng.coreapi.ChatParamsBody;
import cn.xiaoneng.uiapi.Ntalker;
import de.greenrobot.event.EventBus;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * @author yaomingLu
 * @version V4.0.1
 * @since 2015-4-2
 * <p/>
 * <pre>
 * 抽象基类
 * </pre>
 */
public abstract class GBaseActivity extends FragmentActivity implements
        OnClickListener {
    public String TAG = "";
    /**
     * 主内容布局.
     */
    private FrameLayout mMiddleContent;
    public RelativeLayout mTopbar,relativeLayoutGlobalMain,mBottomBar;
    private ImageView mLeftImg,imageViewTopBarBg;
    private ImageView mRightImg;
    private TextView mLeftText;
    private TextView mRightText;
    private TextView mTitleText;
    /**
     * 主标题布局.
     */
    private RelativeLayout mtitleContent;
    //public LoadingDialog progressDlg;
    public boolean isOnStopActivity;
    private PopupWindow mPopupWindow;

    public boolean isUsePx;
    private ImageView mBottomLine;
    private ImageView mCenterImg;
    private LinearLayout mLeftDefineLinearlayout;
    private LinearLayout mRightDefineLinearlayout;
    private ImageView imageView_blur_home;

    public RelativeLayout relativeLayout_blur_home,relativeLayoutShopCar;
    private ImageView imageViewT1;
    private LinearLayout linearLayoutT2;
    private ImageView imageViewTelCall;
    private ImageView imageViewChat;
    private ImageView imageViewClose;

    private ChangeTextIcon textIcon1;
    private ChangeTextIcon textIcon2;
    private ChangeTextIcon textIcon3;
    private ChangeTextIcon textIcon4;
    private ChangeTextIcon textIcon5;
    public ArrayList<ChangeTextIcon> textIcons;
    private TextView textViewShopCarCount;
    public static  String Xn_ID="";
    /**
     * ********************************* start GBaseActivity的生命周期 ***********************************************************************
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getName();
        if (Constant.SystemContext.isDebugShowToast) {
            App.showToast(TAG);
            AbLogUtil.e("当前activity", TAG);
        }

        requestWindowFeature(Window.FEATURE_NO_TITLE);// 将标题栏去掉
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        super.setContentView(R.layout.activity_base);
        AppManager.getAppManager().addActivity(this); // 将activity推入管理栈
        initTopBar();
        initBottomBar();
        initMiddleView();
    }

    /**
     * 子类调用此方法前会先调用BaseActivity的onCreate初始化各种view
     */
    @Override
    public void setContentView(int layoutResID) {
        View v = null;
        try {
            v = View.inflate(this, layoutResID, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LayoutParams params = mMiddleContent.getLayoutParams();
        params.width = LayoutParams.FILL_PARENT;
        params.height = LayoutParams.FILL_PARENT;
        if (v != null) {
            mMiddleContent.addView(v, params);
        } else {
            v = View.inflate(this, layoutResID, null);
            mMiddleContent.addView(v, params);
        }

    }

    /**
     * 这方法在写xml注意在720*1280上面设置像素px
     */
    public void setContentViewFor720_1280(int layoutResID) {

        setContentView(layoutResID);
        isUsePx = true;
    }

    public void setContentView(View v) {
        LayoutParams params = mMiddleContent.getLayoutParams();
        params.width = LayoutParams.FILL_PARENT;
        params.height = LayoutParams.FILL_PARENT;
        mMiddleContent.addView(v, params);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isOnStopActivity = false;
        MaterialListAdapter.isNeedSacleItem = isUsePx;
        JPushInterface.onResume(this);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPause(this);
        JPushInterface.onPause(this);

    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);
        MaterialListAdapter.isNeedSacleItem = isUsePx;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isOnStopActivity = true;
    }

    @Override
    protected void onDestroy() { // 销毁时activity出栈
        AppManager.getAppManager().finishActivity(this);
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        AbLogUtil.e("onRestoreInstanceState", this.getClass().getSimpleName());
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }

    /************************************ end GBaseActivity的生命周期 ************************************************************************/


    /**
     * 显示加载框
     */
    public void showLoadingDialog() {
        /*if (progressDlg != null && progressDlg.isShowing()) {
			
		} else {
			if (!this.isFinishing() && !isOnStopActivity) {
				progressDlg = LoadingDialog.create(this);
				progressDlg.show();
			}
		}*/
        SweetDialogManager.getInstance().showDialogByProgress(this, "Loading");
    }

    /**
     * 关闭加载框
     */
    public void dismissDialog() {
		/*if (progressDlg != null && progressDlg.isShowing()
				&& !this.isFinishing()) {
			progressDlg.cancel();
		}*/
        SweetDialogManager.getInstance().dismissSweetDialog(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (Constant.SystemContext.isDebugLog) {
            menu.add(0, 0, 0, "请求/响应日志");
            menu.add(1, 1, 1, "crash闪退日志");
            menu.add(2, 2, 2, "showRequestToast");
            menu.add(3, 3, 3, "商家版");
            menu.add(4, 4, 4, "掌柜版");
            menu.add(5, 5, 5, "chat");
            menu.add(6, 6, 6, "发送公告");
        }
        return true;
    }

    /**
     * 部分手机无法使用菜单键 ，可以从此处进入
     */
    /**
     *
     */
    @SuppressWarnings("deprecation")
    private void initTestPopupWindow() {
        LinearLayout contentview = (LinearLayout) getLayoutInflater().inflate(
                R.layout.testpopupwindow, null);
        Button button1 = (Button) contentview.findViewById(R.id.button1);
        Button button2 = (Button) contentview.findViewById(R.id.button2);
        Button button3 = (Button) contentview.findViewById(R.id.button3);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // response
                startActivity(new Intent(getApplicationContext(),
                        ServerLogActivity.class));
                mPopupWindow.dismiss();
            }
        });
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // crash
                startActivity(new Intent(getApplicationContext(),
                        ServerLogActivity.class).putExtra("crash", "crash"));
                mPopupWindow.dismiss();
            }
        });
        button3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // uitest
			/*	startActivity(new Intent(getApplicationContext(),
						TestFunctionActivity2.class));*/
                if(Constant.SystemContext.isDebugShowToast) {
                    Constant.SystemContext.isDebugShowToast=false;
                }else
                {
                    Constant.SystemContext.isDebugShowToast=true;
                }


                mPopupWindow.dismiss();
            }
        });
        mPopupWindow = new PopupWindow(contentview,
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.color.white));
        mPopupWindow.setOutsideTouchable(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                startActivity(new Intent(getApplicationContext(),
                        ServerLogActivity.class));
                break;
            case 1:
                startActivity(new Intent(getApplicationContext(),
                        ServerLogActivity.class).putExtra("crash", "crash"));
                break;
            case 2:
                if (Constant.SystemContext.isDebugShowToast) {
                    Constant.SystemContext.isDebugShowToast = false;
                } else {
                    Constant.SystemContext.isDebugShowToast = true;
                }
                // gotoActivity(SuperProjectDetailActivity.class, null);
                //gotoActivity(TestActivity.class, null);
			/*Intent intent = new Intent(getActivity(), WebActivity.class);
			intent.putExtra(WebActivity.Title, "广告预览");
			intent.putExtra(WebActivity.RightTest, "分享");
			intent.putExtra(WebActivity.WebUrl, "http://192.168.0.138:8080/GmallBaseServer/gmall/P-center/turnover.jsp");
			startActivity(intent);*/

                break;
            case 3:
			/*startActivity(new Intent(getApplicationContext(),
					HomeActivity_shangjia.class));
			if (!AppManager.getAppManager().currentActivity().toString().contains("HomeActivity_shangjia")) {
				finish();
			}*/

                break;
            case 4:
			/*startActivity(new Intent(getApplicationContext(),
					HomeActivity.class));
			if (!AppManager.getAppManager().currentActivity().toString().contains("HomeActivity")) {
				finish();
			}*/
                break;
            case 5:
				/*startActivity(new Intent(getApplicationContext(),
						SelectChatActivity.class));
				*/
                break;

        }

        return true;
    }

    /************************************ end 加载对话框 与 基础菜单功能 ************************************************************************/

    /************************************ start GBaseActivity的标题栏设置方式 ************************************************************************/
    /**
     * 初始化中间内容
     */
    private void initMiddleView() {
        mMiddleContent = (FrameLayout) findViewById(R.id.ui_content);
    }

    /**
     * @return 获取整个顶部栏
     */
    public View getTopBar() {
        return mTopbar;
    }
    /**
     * @return 获取全局布局
     */
    public RelativeLayout getRelativeLayoutGlobalMain() {
        return relativeLayoutGlobalMain;
    }
    /**
     * @return 只获取中间的标题
     */
    public TextView getTopTitle() {
        return mTitleText;
    }

    /**
     * 初始化标题栏
     */
    private void initTopBar() {
        mTopbar = (RelativeLayout) findViewById(R.id.topbar);
        mtitleContent = (RelativeLayout) findViewById(R.id.ll_title_content);
        mLeftImg = (ImageView) findViewById(R.id.iv_leftimg);
        mRightImg = (ImageView) findViewById(R.id.iv_rightimg);
        mBottomLine = (ImageView) findViewById(R.id.bottomline);
        mCenterImg = (ImageView) findViewById(R.id.iv_centerimg);
        mLeftText = (TextView) findViewById(R.id.tv_lefttext);
        mRightText = (TextView) findViewById(R.id.tv_righttext);
        mTitleText = (TextView) findViewById(R.id.tv_title);
        mLeftDefineLinearlayout = (LinearLayout) findViewById(R.id.linearlayout_left_define);
        mRightDefineLinearlayout = (LinearLayout) findViewById(R.id.linearlayout_right_define);
        relativeLayoutGlobalMain= (RelativeLayout) findViewById(R.id.relativeLayoutGlobalMain);
        imageViewTopBarBg=(ImageView) findViewById(R.id.imageViewTopBarBg);
        imageView_blur_home=(ImageView)findViewById(R.id.imageView_blur_home);
        mTitleText.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
        mRightImg.setOnClickListener(this);
        mLeftText.setOnClickListener(this);
        mRightText.setOnClickListener(this);
        mTitleText.setOnClickListener(this);
        mCenterImg.setOnClickListener(this);

        AbViewUtil.scaleContentView(mTopbar);

    }
    public  void bottomButtonDefault() {
        textIcon1.setIconAlpha(0);
        textIcon2.setIconAlpha(0);

        textIcon4.setIconAlpha(0);
        textIcon5.setIconAlpha(0);
    }
    private void initBottomBar() {
        relativeLayoutShopCar= (RelativeLayout) findViewById(R.id.relativeLayoutShopCar);
        relativeLayout_blur_home = (RelativeLayout) findViewById(R.id.relativeLayout_blur_home);relativeLayout_blur_home.setOnClickListener(this);
        AbViewUtil.scaleContentView(relativeLayout_blur_home);
        relativeLayout_blur_home.setVisibility(View.GONE);
        imageViewT1 = (ImageView) findViewById(R.id.imageViewT1);
        linearLayoutT2 = (LinearLayout) findViewById(R.id.linearLayoutT2);
        imageViewTelCall = (ImageView) findViewById(R.id.imageViewTelCall);imageViewTelCall.setOnClickListener(this);
        imageViewChat = (ImageView) findViewById(R.id.imageViewChat);imageViewChat.setOnClickListener(this);
        imageViewClose = (ImageView) findViewById(R.id.imageViewClose);imageViewClose.setOnClickListener(this);


        mBottomBar = (RelativeLayout) findViewById(R.id.bottom_bar);
        AbViewUtil.scaleContentView(mBottomBar);
        textViewShopCarCount=(TextView) findViewById(R.id.textViewShopCarCount);
        textIcon1 = (ChangeTextIcon) findViewById(R.id.textIcon1);
        textIcon2 = (ChangeTextIcon) findViewById(R.id.textIcon2);
        textIcon3 = (ChangeTextIcon) findViewById(R.id.textIcon3);
        textIcon4 = (ChangeTextIcon) findViewById(R.id.textIcon4);
        textIcon5 = (ChangeTextIcon) findViewById(R.id.textIcon5);
        textIcons = new ArrayList<ChangeTextIcon>();
        textIcons.add(textIcon1);
        textIcons.add(textIcon2);
        textIcons.add(textIcon3);
        textIcons.add(textIcon4);
        textIcons.add(textIcon5);
        textIcon1.setIconAlpha(1);
        textIcon1.setOnClickListener(this);
        textIcon2.setOnClickListener(this);
        textIcon3.setOnClickListener(this);
        textIcon4.setOnClickListener(this);
        textIcon5.setOnClickListener(this);

        mBottomBar.setVisibility(View.GONE);
        //设置购物车红色数量
        RefreshShopCarRedCount(textViewShopCarCount,relativeLayoutShopCar);
    }
    public static void RefreshShopCarRedCount(TextView textView,RelativeLayout relativeLayout) {


        if (App.getShopCarTotalCount()>99)
        {
            relativeLayout.setBackgroundResource(R.drawable.bg_red_number_tyty);
            RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) textView.getLayoutParams();
            layoutParams.setMargins(AbViewUtil.scaleValue(App.getInstance().getApplicationContext(),5),0,AbViewUtil.scaleValue(App.getInstance().getApplicationContext(),5),0);
        }else
        {
            relativeLayout.setBackgroundResource(R.drawable.bg_red_number_ty);
            RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) textView.getLayoutParams();
            layoutParams.setMargins(0,0,0,0);
        }

        if (App.getShopCarList()!=null && App.getShopCarList().size()>0 && App.getShopCarTotalCount()>0) {
            textView.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);

            String countStr=App.getShopCarTotalCount()+"";

            if (App.getShopCarTotalCount()>99) {
                countStr = "99+";
            }
            textView.setText(countStr);
            textView.invalidate();
        }else
        {
            textView.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.GONE);
        }

    }
    public  void updateBottomShopCarCount() {

        RefreshShopCarRedCount(textViewShopCarCount,relativeLayoutShopCar);
    }
    public RelativeLayout getBottomBar() {
        return mBottomBar;
    }

    public void getBottomBarShowOtherActivity() {
        mBottomBar.setVisibility(View.VISIBLE);
        bottomButtonDefault();

    }
    public RelativeLayout getBottomBarToShow(int showIndex) {
        for (int i = 0; i < textIcons.size(); i++) {
            textIcons.get(i).setIconAlpha(0);
        }
        mBottomBar.setVisibility(View.VISIBLE);
        if (showIndex==MainActivity.TAB_GouWuChe || showIndex==MainActivity.TAB_GenRenZhongXin) {
            textIcons.get(showIndex+1).setIconAlpha(1);
        }else
        {
            textIcons.get(showIndex).setIconAlpha(1);
        }

        return mBottomBar;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_leftimg:
                doClickLeftImg();
                break;
            case R.id.iv_rightimg:
                doClickRightImg();
                break;
            case R.id.tv_lefttext:
                doClickLeftText();
                break;
            case R.id.tv_righttext:
                doClickRightText();
                break;
            case R.id.tv_title:
                doClickTitle();
                break;
            case R.id.iv_centerimg:
                doClickCenterImg();
                break;
            case R.id.textIcon1:

                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_ShouYe));
                if (!this.getLocalClassName().contains("MainActivity")) {
                    finish();
                }

                break;
            case R.id.textIcon2:
                Intent intent2 = new Intent(this,MainActivity.class);
                startActivity(intent2);
                EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_FenLei));
                if (!this.getLocalClassName().contains("MainActivity"))
                    finish();
                break;
            case R.id.textIcon3:
             //   Intent intent3 = new Intent(this,MainActivity.class);
            //    startActivity(intent3);
                EventBus.getDefault().post(new UpdateBlurImage(true,this));
                break;
            case R.id.textIcon4:
                Intent intent4 = new Intent(this,MainActivity.class);
                startActivity(intent4);
                EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_GouWuChe));
                if (!this.getLocalClassName().contains("MainActivity"))
                    finish();
                break;
            case R.id.textIcon5:
                if (App.getUserBean()==null)
                {
                    Intent intent5=new Intent(GBaseActivity.this,LoginActivity.class);
                    startActivity(intent5);
                    return;
                }
                EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_GenRenZhongXin));
                if (!this.getLocalClassName().contains("MainActivity"))
                    finish();
                break;
            case R.id.imageViewTelCall:
                if (App.getUserCenterOtherBean()!=null && App.getUserCenterOtherBean().getCustomer_service()!=null)
                {
                    Intent intent6 = new Intent(Intent.ACTION_DIAL,
                            Uri.parse("tel:" + App.getUserCenterOtherBean().getCustomer_service()));
                    startActivity(intent6);
                }else {
                    Intent intent6 = new Intent(Intent.ACTION_DIAL,
                            Uri.parse("tel:" + "020-80738887"));
                    startActivity(intent6);
                }
      /*          HashMap hashMap = new HashMap();
                hashMap.put("医生姓名", card.getDoctorInfo().getDoctor_name());
                hashMap.put("医生电话", card.getDoctorInfo().getPhone_number());
                MobclickAgent.onEventValue(context, "callDoctor", hashMap, 1);
                listDialog.dismiss();*/
                break;
            case R.id.imageViewChat:


                GoToChat();
              //  listDialog.dismiss();
                break;
            case R.id.imageViewClose:


                EventBus.getDefault().post(new UpdateBlurImage(false,this));



                break;
            case R.id.relativeLayout_blur_home:

                break;
            default:
                break;
        }
    }

    public void GoToChat() {
        String siteid = "9704";//企业siteid
        String sdkKey = "39E97583-B6C1-49A6-9BF1-C960045BC5DA";//
      //  String kfuid ="kf_"+siteid+"_"+"ISME9754_T2D"+"_"+Xn_ID;// 指定客服id
        String kfuid =Xn_ID;
        String kfname =kfuid;// 指定客服昵称
        String settingid = kfuid;// 客服组id
        String groupName = kfname;// 客服组名称
        //初始化小能客服SDK
        //       Ntalker.getInstance().initSDK(context.getApplicationContext(), siteid, sdkKey);

   /*             if (App.getInstance().getUserBean()==null || TextUtils.isEmpty(App.getInstance().getUserBean().getUser_name()))
                {
                    listDialog.dismiss();
                    Intent intent = new Intent(context, LoginAndRegisterActivity.class);
                    context.startActivity(intent);
                    return ;
                }
                String userid = App.getInstance().getUserBean().getNick_name();
                String username = App.getInstance().getUserBean().getNick_name();*/
        String userid = "userid";
        String username ="username";
        int userlevel = 1;// 用户级别,级别分为1-5,5为最高级
        int logIn = Ntalker.getInstance()
                .login(userid, username, userlevel);// 登录时调
                 /*   if (0 == logIn) {
                        Toast.makeText(context.getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                        //Log.e("login", "登录成功");
                    } else {
                        Toast.makeText(context.getApplicationContext(), "登录失败，错误码:" + logIn, Toast.LENGTH_SHORT).show();
                        //Log.e("login", "登录失败，错误码:" + logIn);
                    }*/
        ChatParamsBody chatparams = null;

        Ntalker.getInstance().startChat(getApplicationContext(), settingid, "51采药网在线客服", null, null, chatparams,false);
    }

    public void setTopBarBackGround( int  resID) {
        imageViewTopBarBg.setBackgroundColor(resID);
    }
public ImageView getImageViewTopBarBg()
{
    return imageViewTopBarBg;
}
    /**
     * 设置标题栏 左边自定义的布局
     *
     * @param view 自定义布局
     */
    public void setLeftDefinedLinearLayout(int visibility, View view) {
        mLeftDefineLinearlayout.removeAllViews();
        mLeftDefineLinearlayout.addView(view,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mLeftDefineLinearlayout.setVisibility(visibility);
    }



    /**
     * 设置标题栏 右边自定义的布局
     *
     * @param view 自定义布局
     */
    public void setRightDefineLinearLayout(int visibility, View view) {
        mRightDefineLinearlayout.removeAllViews();
        mRightDefineLinearlayout.addView(view,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mRightDefineLinearlayout.setVisibility(visibility);
    }

    /**
     * 设置头部的底部线的可见性，默认不可见
     *
     * @param visible
     */
    public void setBottomLine(int visible) {
        mBottomLine.setVisibility(visible);
    }

    /**
     * 设置topbar的背景资源图 默认高度为50
     *
     * @param resId
     */
    public void setTitleBarBackground(int resId) {
        mtitleContent.setBackgroundResource(resId);
    }

    /**
     * 设置标题文字的颜色
     *
     * @param color
     */
    public void setTitleTextColor(int color) {
        mTitleText.setTextColor(color);
    }

    /**
     * 设置左边字体的颜色
     *
     * @param color
     */
    public void setLeftTextColor(int color) {
        mLeftText.setTextColor(color);
    }

    /**
     * 设置右边字体的颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        mRightText.setTextColor(color);
    }

    /**
     * 设置左边的图片,已经默认设置点击事件为关闭当前页面
     *
     * @param visibility
     * @param resId
     */
    public void setLeftImg(int visibility, int resId) {
        mLeftImg.setVisibility(visibility);
        mLeftImg.setImageResource(resId);
    }

    /**
     * 设置右边的图片
     *
     * @param visibility
     * @param resId
     */
    public void setRightImg(int visibility, int resId) {
        mRightImg.setVisibility(visibility);
        mRightImg.setImageResource(resId);
    }

    /**
     * 设置左边的文字
     *
     * @param visibility
     * @param resId
     */
    public void setleftText(int visibility, int resId) {
        mLeftText.setVisibility(visibility);
        mLeftText.setText(resId);
    }

    /**
     * 设置左边的文字
     *
     * @param visibility
     * @param string
     */
    public void setleftText(int visibility, String string) {
        mLeftText.setVisibility(visibility);
        mLeftText.setText(string);
    }

    /**
     * 设置右边的文字
     *
     * @param visibility
     * @param resId
     */
    public void setRightText(int visibility, int resId) {
        mRightText.setVisibility(visibility);
        mRightText.setText(resId);
    }

    /**
     * 设置右边的文字
     *
     * @param visibility
     * @param string
     */
    public void setRightText(int visibility, String string) {
        mRightText.setVisibility(visibility);
        mRightText.setText(string);
    }

    /**
     * 设置中间的title
     *
     * @param resId
     */
    public void setTopTitle(int resId) {
        mTitleText.setVisibility(View.VISIBLE);
        mTitleText.setText(resId);
    }

    /**
     * 设置中间的title
     *
     * @param text
     */
    public void setTopTitle(String text) {
        mTitleText.setVisibility(View.VISIBLE);
        mTitleText.setText(text);
    }

    /**
     * 设置中间img
     *
     * @param visibility
     * @param resId
     */
    public void setCenterImg(int visibility, int resId,int width,int height) {
        mCenterImg.setVisibility(visibility);
        mCenterImg.getLayoutParams().width=width;
        mCenterImg.getLayoutParams().height=height;
        mCenterImg.setImageResource(resId);

    }

    /**
     * @return 标题栏文本
     */
    public String getTopTitleText() {
        return mTitleText.getText().toString();
    }

    /**
     * @return 获取左边标题文本
     */
    public String getLeftText() {
        return mLeftText.getText().toString();
    }

    /**
     * @return 获取右边的标题文本
     */
    public String getRightText() {
        return mRightText.getText().toString();
    }

    public TextView getRightTextView() {
        return mRightText;
    }

    public ImageView getRightImageView() {
        return mRightImg;
    }

    /**
     * 点击中间的title
     */
    public void doClickTitle() {
        // 开放给子类覆盖
        if (Constant.SystemContext.isDebugLog) {
            showDropDownPopupWindow();
        }
    }

    /**
     * 点击中间的img
     */
    private void doClickCenterImg() {
        // 开放给子类覆盖
        showDropDownPopupWindow();
    }

    /**
     * 点击右边text
     */
    public void doClickRightText() {
        // 开放给子类覆盖
    }

    /**
     * 点击左边text
     */
    public void doClickLeftText() {
        // 开放给子类覆盖
    }

    /**
     * 点击右边图标
     */
    public void doClickRightImg() {
        // 开放给子类覆盖
    }

    /**
     * 点击左边图标
     */
    public void doClickLeftImg() {
        // 开放给子类覆盖,默认关闭当前页面
        finish();
    }

    /************************************ end GBaseActivity的标题栏设置方式 ************************************************************************/

    /************************************ start GBaseActivity的其他通用设置 ************************************************************************/
    /**
     * 跳转Activity
     *
     * @param activity
     * @param bundle
     */
    protected void gotoActivity(Class<? extends Activity> activity,
                                Bundle bundle) {
        Intent intent = new Intent(this, activity);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 获取当前activity实例
     *
     * @return
     */
    protected Activity getActivity() {
        return this;
    }

    /**
     * 显示popup菜单
     */
    private void showDropDownPopupWindow() {
        if (Constant.SystemContext.isDebugLog) {
            // 部分机型没有菜单键，此处作为查看日志的入口
            if (mPopupWindow == null) {
                initTestPopupWindow();
            }
            mPopupWindow.showAsDropDown(mTopbar, 0, 0);
            Toast.makeText(this, "debug模式开启中，可查看日志和测试ui", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isUsePx) {
            AbViewUtil.scaleContentView(mMiddleContent);
            MaterialListAdapter.isNeedSacleItem = isUsePx;
        }
    }
    public String getCurrentActivityName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);


        // get the info from the currently running task
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);


        ComponentName componentInfo = taskInfo.get(0).topActivity;
        return componentInfo.getClassName();
    }


    /**
     * @param activity
     * @param scale    截屏缩放系数，加快速度
     * @return
     */

    private Bitmap takeScreenShot(Activity activity, float scale) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        b1 = AbImageUtil.getScaleBitmap(b1, scale);
        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        // 获取屏幕长和高
        //int width = AppConfig.getInstance().screenWidth;
        // int height =AppConfig.getInstance().screenHeight;
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, (int) (statusBarHeight * scale), b1.getWidth(), (int) (b1.getHeight()
                - statusBarHeight * scale));
        view.destroyDrawingCache();
        return b;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      //  EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    public void onEventMainThread(final UpdateBlurImage updateBlurImage) {
        // TODO Auto-generated method stub

        if (updateBlurImage.getContext().toString().equals(getActivity().toString())) {
            //图片已经生成了
            if (!updateBlurImage.isShowBlurImage()) {
                relativeLayout_blur_home.setVisibility(View.GONE);
                getBottomBar().setVisibility(View.VISIBLE);



                ValueAnimator colorAnim = ObjectAnimator.ofInt(imageView_blur_home, "alpha", 255, 0);
                colorAnim.setDuration(800);
                colorAnim.start();

                ValueAnimator anim = ObjectAnimator.ofInt(imageViewChat, "alpha", 255, 0);
                anim.setDuration(800);// 动画持续时间
                anim.start();

                ValueAnimator anim3 = ObjectAnimator.ofInt(imageViewTelCall, "alpha", 255, 0);
                anim3.setDuration(800);// 动画持续时间
                anim3.start();

                ValueAnimator anim4 = ObjectAnimator.ofInt(imageViewT1, "alpha", 255, 0);
                anim4.setDuration(800);// 动画持续时间
                anim4.start();
                return;
            }
            new Thread(new Runnable() {

                @Override
                public void run() {
                    final Bitmap newImg = Blur.fastblur(getActivity(), takeScreenShot(getActivity(), 0.2f), 10);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            imageView_blur_home.setVisibility(View.VISIBLE);
                            relativeLayout_blur_home.setVisibility(View.VISIBLE);
                            getBottomBar().setVisibility(View.GONE);
                            imageView_blur_home.setImageBitmap(newImg);

              /*              ValueAnimator colorAnim2 = ObjectAnimator.ofInt(getBottomBar(), "alpha", 255, 0);
                            colorAnim2.setDuration(1500);
                            colorAnim2.start();
*/

                            ValueAnimator colorAnim = ObjectAnimator.ofInt(imageView_blur_home, "alpha", 0, 255);
                            colorAnim.setDuration(800);
                            colorAnim.start();

                            ValueAnimator anim = ObjectAnimator.ofInt(imageViewChat, "alpha", 0, 255);
                            anim.setDuration(800);// 动画持续时间
                            anim.start();

                            ValueAnimator anim3 = ObjectAnimator.ofInt(imageViewTelCall, "alpha", 0, 255);
                            anim3.setDuration(800);// 动画持续时间
                            anim3.start();

                            ValueAnimator anim4 = ObjectAnimator.ofInt(imageViewT1, "alpha", 0, 255);
                            anim4.setDuration(800);// 动画持续时间
                            anim4.start();

                        }
                    });


                }
            }).start();
        }
    }
}
